// 封装完毕后，如果需要做爬虫，只需要以下几步：
// 1.写一个爬虫类，继承spider
// 2.实现onCatchHTML方法
// 3.使用：创建爬虫对象，传入URL即可
const cheerio = require("cheerio");
const download = require("download");
let HOST = "https://web.itheima.com/";

import Spider from "./spider";
export default class Photos extends Spider {
  oncatchHTML(result: string) {
    // 由子类具体实现
    // console.log(result);
    let $ = cheerio.load(result);
    console.log(result);


    let imgs = Array.prototype.map.call(
      $(".bili-video-card__cover > .v-img"),
      item => HOST + encodeURI($(item).attr("src"))
    );

    Promise.all(imgs.map(v => download(v, "dist")))
      .then(() => {
        console.log("图片下载完成！");
      })
      .catch(err => {
        console.log(err);
      });
  }
}

new Photos({
  // url: 'https://www.bilibili.com/?spm_id_from=333.1007.0.0',
  url: 'http://h5.dooring.cn/blog/china',
  //   url: `${HOST}teacher.html#ajavaee`
  // "https://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json"
});
// url:"http://123.56.5.146/#/login",
